﻿Imports ComponentFactory.Krypton.Toolkit
Imports WiseLibraries.Net

Public Class Login
    Inherits KryptonForm

    Private quantErro As Integer = 0

    Public Shared usuario As Usuario = Nothing

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub

    Private Sub btnEntrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEntrar.Click
        Dim u As New Usuario(txtUsuario.Text, txtSenha.Text)
        If (u.login()) Then
            usuario = u
            quantErro = 0
            Me.Close()
        Else

            KryptonMessageBox.Show("Nome de usuário/senha inválido. Verifique e tente novamente", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            quantErro += 1

            If (quantErro = 3) Then
                quantErro -= 1 'Dá mais uma chance para o usuário digitar as informações
                Dim resp = KryptonMessageBox.Show("Você digitou informações incorretas por 3 vezes." & vbNewLine & "Deseja receber os seus dados de usuario por email?", "Erro no usuario", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If (resp = Windows.Forms.DialogResult.Yes) Then
                    Dim frm As New EnviarEmail()
                    frm.ShowDialog()
                End If
            End If
        End If

    End Sub

    Private Sub Login_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        If (usuario IsNot Nothing) Then
            Me.DialogResult = Windows.Forms.DialogResult.OK 'Login foi feito com êxito
        Else
            Me.DialogResult = Windows.Forms.DialogResult.No 'Login falhou
        End If
    End Sub

    Private Sub lblRecuperarSenha_LinkClicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblRecuperarSenha.LinkClicked
        Dim frm As New EnviarEmail()
        frm.ShowDialog()
    End Sub

    Private Sub Login_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If (e.KeyChar = ChrW(Keys.Enter)) Then
            btnEntrar.PerformClick()

        ElseIf (e.KeyChar = ChrW(Keys.Escape)) Then
            Me.Close()
        End If
    End Sub
End Class